<?php
namespace app\stores\model\withdraw;
use app\common\model\admin\AdminModel;
use app\common\model\CommonModel;
use app\platform\controller\admin\Promotionteam;
use app\stores\model\promotion\TeamModel;
use think\facade\Db;

class MemberWithdrawModel extends CommonModel{

    protected  $name = 'member_withdraw';

    protected  $pk   = 'withdraw_id';

    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function info($where){
        $info= self::where($where)->find();
        return $info;
    }

    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('member_withdraw')->alias('mw')
            ->leftjoin('beautiful_member m','m.member_id=mw.member_id')
            ->leftjoin('member_withdraw_account wa','wa.withdraw_account_id=mw.withdraw_account_id')
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->whereIn('mw.status',$where['status']);
            })->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('m.nick_name','like','%'.$where['name'].'%');
            })->when(isset($where['is_delete']) && !empty($where['is_delete']),function ($query) use ($where){
                $query->where('mw.is_delete','=',$where['is_delete']);
            })->when(isset($where['type']) && !empty($where['type']),function ($query) use ($where){
                $query->where('mw.type','=',$where['type']);
            })->when(isset($where['member_id']) && !empty($where['member_id']),function ($query) use ($where){
                $query->where('w.member_id','=',$where['member_id']);
            })->field('mw.team_id,mw.member_id,mw.remarks,mw.operate_id,mw.withdraw_id,mw.status,mw.money,mw.fee,mw.before_money-mw.after_money-mw.fee as received_money,mw.pay_time,mw.pay_voucher,mw.add_time,
            wa.type as settlement_type,wa.open_bank_name,wa.open_branch,wa.account_name,wa.account_number,m.nick_name,m.face,m.member_num,m.mobile');
        $count = $query->count();
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        if($list){
            foreach ($list as $k=>$v){
                $v['team_name'] = null;
                if($v['member_id']<1){
                    $team = TeamModel::where(['id'=>$v['team_id']])->find();
                    if($team){
                        $list[$k]['team_name'] = $team['name'];
                        $list[$k]['nick_name'] = $team['name'].'+'.$team['username'];
                    }

                }
                if($v['status']==1) $list[$k]['settlement_type'] = $v['pay_voucher'];
                $list[$k]['settlement_price'] = $v['money'];
                $list[$k]['settlement_time'] = $v['status']==1?date('Y-m-d',strtotime($v['pay_time'])):null;
                $admin= AdminModel::find($v['operate_id']);
                $list[$k]['name'] = $admin?$admin['username']:null;

            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
}
